// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/type/signature.h>
#include <turbo/log/check.h>
#include <turbo/strings/string_builder.h>
#include <turbo/strings/str_join.h>

namespace cantor {
    void type_signature(const pb::TypeInfo &info, turbo::MD5 &md5) {
        turbo::StringBuilder builder;
        auto type = info.type();
        builder << pb::TypeKind_Name(type);
        switch (type) {
            case pb::TypeKind::TYPE_KIND_INT8:
            case pb::TypeKind::TYPE_KIND_INT16:
            case pb::TypeKind::TYPE_KIND_INT32:
            case pb::TypeKind::TYPE_KIND_INT64:
            case pb::TypeKind::TYPE_KIND_INT128:
            case pb::TypeKind::TYPE_KIND_UINT8:
            case pb::TypeKind::TYPE_KIND_UINT16:
            case pb::TypeKind::TYPE_KIND_UINT32:
            case pb::TypeKind::TYPE_KIND_UINT64:
            case pb::TypeKind::TYPE_KIND_UINT128:
            case pb::TypeKind::TYPE_KIND_FP8:
            case pb::TypeKind::TYPE_KIND_FP16:
            case pb::TypeKind::TYPE_KIND_FP32:
            case pb::TypeKind::TYPE_KIND_FP64:
            case pb::TypeKind::TYPE_KIND_STRING:
            case pb::TypeKind::TYPE_KIND_BINARY:
            case pb::TypeKind::TYPE_KIND_TIMESTAMP:
            case pb::TypeKind::TYPE_KIND_DURATION:
            case pb::TypeKind::TYPE_KIND_BOOLEAN:
            case pb::TypeKind::TYPE_KIND_DATETIME:
            case pb::TypeKind::TYPE_KIND_INTERVAL:
            case pb::TypeKind::TYPE_KIND_DECIMAL64:
            case pb::TypeKind::TYPE_KIND_DECIMAL128:
            case pb::TypeKind::TYPE_KIND_JSON: {
                md5.update(builder.str());
                break;
            }
            case pb::TypeKind::TYPE_KIND_ENUM: {
                builder << "ENUM:" << info.enum_type().uri();
                md5.update(builder.str());
                break;
            }
            case pb::TypeKind::TYPE_KIND_ARRAY: {
                builder << "ARRAY:";
                md5.update(builder.str());
                type_signature(info.array_type().type(), md5);
                break;
            }
            case pb::TypeKind::TYPE_KIND_MAP: {
                builder << "MAP:";
                md5.update(builder.str());
                type_signature(info.map_type().key(), md5);
                type_signature(info.map_type().value(), md5);
                break;
            }
            case pb::TypeKind::TYPE_KIND_SET: {
                builder << "SET:";
                md5.update(builder.str());
                type_signature(info.set_type().key(), md5);
                break;
            }

            case pb::TypeKind::TYPE_KIND_STRUCT: {
                builder << "STRUCT:[" << turbo::str_join(info.struct_type().names(), ",") << "]";
                md5.update(builder.str());
                for (auto &it: info.struct_type().types()) {
                    type_signature(it, md5);
                }
                break;
            }
            case pb::TypeKind::TYPE_KIND_TUPLE: {
                builder << "TUPLE:";
                md5.update(builder.str());
                for (auto &it: info.tuple_type().types()) {
                    type_signature(it, md5);
                }
                break;
            }

            case pb::TypeKind::TYPE_KIND_UNION: {
                builder << "UNION:[" << turbo::str_join(info.struct_type().names(), ",") << "]";
                md5.update(builder.str());
                for (auto &it: info.struct_type().types()) {
                    type_signature(it, md5);
                }
                break;
            }

            case pb::TypeKind::TYPE_KIND_UDF: {
                /// using real type
                KCHECK(false)<<"udf type must be resolved before signature";
            }
            case pb::TypeKind::TYPE_KIND_PROTO: {
                builder << "PROTO:" << info.proto_type().uri();
                md5.update(builder.str());
                break;
            }
            default:
                break;
        }
    }
}
